﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;
using System.Reflection;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.MessageInfo
{
    public partial class frmMaintainMessageBox : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        private MstMessageEntity mMessage;
        private List<MstPersonEntity> mListToPersons;
        #endregion

        #region Constructor
        public frmMaintainMessageBox()
        {
            InitializeComponent();
            if (mMessage == null)
            {
                mMessage = new MstMessageEntity();
                mMessage.MessageId = Guid.NewGuid().ToString();
            }
        }
        public frmMaintainMessageBox(MstMessageEntity entity)
        {
            InitializeComponent();
            if (entity != null)
            {
                mMessage = entity;
                mListToPersons = MstPersonMessageBs.GetToList(mMessage.MessageId);
            }
            else
            {
                mMessage = new MstMessageEntity();
                mMessage.MessageId = Guid.NewGuid().ToString();
            }
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbShowMessageLevel, "ML", true);
        }
        private void SetToPersonText()
        {
            if (mListToPersons != null)
            {
                foreach (MstPersonEntity entity in mListToPersons)
                {
                    if (string.IsNullOrEmpty(txtShowMessageReceiver.Text))
                    {
                        txtShowMessageReceiver.Text = entity.NameCn;
                    }
                    else
                    {
                        txtShowMessageReceiver.Text += "; " + entity.NameCn;
                    }
                }
            }
            else
            {
                txtShowMessageReceiver.Text = "";
            }
        }
        private void SetFields()
        {
            if (mMessage == null)
            {
                return;
            }
            txtShowMessageSender.Text = "";
            if (ThisActionType == ActionType.New)
            {
                txtShowMessageSender.Text = CommonParameter.CurrentUser.PersonEntity.NameCn + " (" + CommonParameter.CurrentUser.PersonEntity.PersonCode + ")";
            }
            else
            {
                MstPersonEntity sender = MstPersonBs.GetMstPersonByID(mMessage.MessageSender);
                if (sender != null)
                {
                    txtShowMessageSender.Text = sender.NameCn + " (" + sender.PersonCode + ")";
                }
            }
            txtShowMessageTitle.Text = mMessage.MessateTitle;
            if (mMessage.MessageDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                txtShowMessageDate.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            }
            else
            {
                txtShowMessageDate.Text = mMessage.MessageDate.ToString("yyyy-MM-dd hh:mm:ss");
            }
            txtShowMessageReceiver.Text = "";
            if (string.IsNullOrEmpty(mMessage.MessageId) == false)
            {
                mListToPersons = MstPersonMessageBs.GetToList(mMessage.MessageId);
                SetToPersonText();
            }
            cmbShowMessageLevel.SetSelectedValue(mMessage.MessageLevel);
            txtShowMessageBody.Text = mMessage.MessageBody;
        }
        private void SetEntity()
        {
            mMessage.MessateTitle = txtShowMessageTitle.Text;
            mMessage.MessageDate = DateTime.Now;
            mMessage.MessageSender = CommonParameter.CurrentUser.PersonId;
            if (cmbShowMessageLevel.SelectedValue != null)
            {
                mMessage.MessageLevel = cmbShowMessageLevel.SelectedValue.ToString();
            }
            mMessage.MessageBody = txtShowMessageBody.Text;
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtShowMessageTitle.Text))
            {
                MessageBox.Show("消息标题不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtShowMessageReceiver.Text))
            {
                MessageBox.Show("收消息人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbShowMessageLevel.Text))
            {
                MessageBox.Show("消息级别不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtShowMessageBody.Text))
            {
                MessageBox.Show("消息内容不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = MstMessageBs.SendMessage(mMessage, mListToPersons);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("发送消息失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetFields();
        }

        private void txtShowMessageReceiver_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            frm.MultiSelected = true;
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPersonList != null)
            {
                mListToPersons = frm.SelectedPersonList;
                SetToPersonText();
            }
            else
            {
                mListToPersons = null;
                SetToPersonText();
            }
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
        #endregion
    }
}
